package com.zmn.plat.admin.controller.orderconfig.returnvisit.question;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.orderconfig.returnvisitcontent.*;

/**
 * @author linfeng
 * @description：回访问题配置
 * @since 2020/04/16 16:49
 * @modifier
 **/
@Controller
@RequestMapping("/orderconfig/returnvisit/question/")
public class ReturnVisitQuestionListController extends ReturnVisitQuestionBaseController {
    private Logger logger = LoggerFactory.getLogger(ReturnVisitQuestionListController.class);

    /**
     * 描述: 回访问题列表
     *
     * @param query
     *            查询实体
     * @return ModelAndView
     * @author linfeng
     * @since 15:38 2020/04/16
     */
    @SuppressWarnings("Duplicates")
    @RequestMapping(value = "list")
    @RequiresPermissions("/orderconfig/returnvisit/question/list.action")
    public ModelAndView list(@ModelAttribute ReturnVisitQuestionQuery query) {
        ModelAndView mav = new ModelAndView("orderconfig/returnvisit/question/list");
        List<ReturnVisitQuestion> items = returnVisitQuestionService.listPageByQuery(query);
        // 回访类别
        List<ReturnVisitCategory> categoryList = returnVisitCategoryService.listByQuery(new ReturnVisitCategoryQuery());
        List<ReturnVisitAnswer> answerList = returnVisitAnswerService.listByQuery(new ReturnVisitAnswerQuery());
        Map<Integer, String> platMap =
            GlobalConsts.getPlatList().stream().collect(Collectors.toMap(item -> item.getKey(), item -> item.getValue(), (p, u) -> p));
        Map<Integer, String> categoryMap =
            categoryList.stream().collect(Collectors.toMap(item -> item.getCategId(), item -> item.getName(), (p, u) -> p));
        Map<Integer, String> answerMap =
            answerList.stream().collect(Collectors.toMap(item -> item.getAnswerId(), item -> item.getAnswer(), (p, u) -> p));
        mav.addObject("items", items);
        mav.addObject("vo", query);
        mav.addObject("bizTypeList", com.zmn.plat.common.dictionary.ProductDict.listBizType());
        mav.addObject("platMap", platMap);
        mav.addObject("categoryMap", categoryMap);
        mav.addObject("answerMap", answerMap);
        mav.addObject("questionTypeList", ProductDict.getQuestionTypeList());
        mav.addObject("returnVisitCategorys", categoryList);
        return mav;
    }

    /**
     * 功能描述 ：详情页面
     *
     * @param id
     * @return org.springframework.web.servlet.ModelAndView
     * @author linfeng
     * @since 2020/04/16
     * @modifier
     */
    @RequestMapping(value = "detail")
    @RequiresPermissions("/orderconfig/returnvisit/question/detail.action")
    public ModelAndView detail(@RequestParam(value = "id", defaultValue = "0") Integer id) {
        ModelAndView mav = new ModelAndView("orderconfig/returnvisit/question/detail");
        ReturnVisitQuestion returnVisitQuestion = new ReturnVisitQuestion();
        if (id == 0) {
            returnVisitQuestion = new ReturnVisitQuestion();
        } else {
            returnVisitQuestion = Optional.ofNullable(returnVisitQuestionService.findById(id)).orElse(returnVisitQuestion);
        }
        if (returnVisitQuestion.getSort() == null) {
            Integer sort = returnVisitQuestionService.findMaxSort();
            sort = sort == null ? 1 : sort + 1;
            returnVisitQuestion.setSort(sort);
        }
        // 回访类别，只显示启用状态的
        ReturnVisitCategoryQuery categoryQuery = new ReturnVisitCategoryQuery();
        categoryQuery.setStatus(GlobalConsts.YES);
        List<ReturnVisitCategory> categoryList = returnVisitCategoryService.listByQuery(categoryQuery);

        // 回访答案，只显示启用状态的
        ReturnVisitAnswerQuery answerQuery = new ReturnVisitAnswerQuery();
        answerQuery.setStatus(GlobalConsts.YES);
        List<ReturnVisitAnswer> answerList = returnVisitAnswerService.listByQuery(answerQuery);

        mav.addObject("question", returnVisitQuestion);
        mav.addObject("returnVisitCategorys", categoryList);
        mav.addObject("returnVisitAnswers", answerList);
        mav.addObject("bizTypeList", com.zmn.plat.common.dictionary.ProductDict.listBizType());
        return mav;
    }
}
